package com.kaluli.config;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages =  {"com.kaluli.project.repository.impl" , "com.kaluli.system.repository"} )

@PropertySource({ "classpath:db.properties" })
public class PersistenceConfig {

	@Autowired
	private Environment env;
	
	@Bean
	public DataSource dataSource() throws SQLException {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(env.getProperty("jdbc.driver"));
		dataSource.setUsername(env.getProperty("jdbc.user"));
		dataSource.setPassword(env.getProperty("jdbc.pass"));
		dataSource.setUrl(env.getProperty("jdbc.url"));
		return dataSource;
	}

	@Bean
	public SessionFactory sessionFactory(@Qualifier("dataSource") DataSource ds) throws IOException {
		LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
		sessionFactoryBean.setDataSource(ds);
		sessionFactoryBean.setPackagesToScan("com.kaluli.project.domain.entity" , "com.kaluli.system.domain.entity");
		sessionFactoryBean.setHibernateProperties(hibernateProperties());
		sessionFactoryBean.afterPropertiesSet();
		return sessionFactoryBean.getObject();
	}

	@Bean
	public HibernateTemplate hibernateTemplate(SessionFactory sessionFactory) {
		HibernateTemplate template = new HibernateTemplate(sessionFactory);
		//template.setFlushMode(FlushMode.COMMIT); 
//		template.setCheckWriteOperations(false);
		return template;
	}

	private Properties hibernateProperties() {
		Properties properties = new Properties();
		properties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
		properties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql"));
		properties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
		properties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto"));
		return properties;
	}

	@Bean
	public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
		//sessionFactory.openSession().setFlushMode(AUTO);
//		Session session=sessionFactory.openSession();
//		FlushMode flushMode=sessionFactory.openSession().getFlushMode();
//		session.setFlushMode(FlushMode.AUTO);
		HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
		return transactionManager;
	}

	@Bean
	public DataSource dataSource2() throws SQLException{
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(env.getProperty("jdbc2.driver"));
		dataSource.setUsername(env.getProperty("jdbc2.user"));
		dataSource.setPassword(env.getProperty("jdbc2.pass"));
		dataSource.setUrl(env.getProperty("jdbc2.url"));
		return dataSource;
	}
	
	
//	@Bean
//	public DetectApiJdbcImpl jdbcDao(@Qualifier("dataSource2") DataSource dataSource){
//		
////		DruidDataSource dataSource = new DruidDataSource();
////		dataSource.setDriverClassName(env.getProperty("jdbc2.driver"));
////		dataSource.setUsername(env.getProperty("jdbc2.user"));
////		dataSource.setPassword(env.getProperty("jdbc2.pass"));
////		dataSource.setUrl(env.getProperty("jdbc2.url"));
//		
//		DetectApiJdbcImpl impl = new DetectApiJdbcImpl();
//		impl.setDataSource(dataSource);
//		return impl;
//	}

}